/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.university.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.university.dao.UniversityDao;
import com.thinkgem.jeesite.modules.university.entity.University;

/**
 * 学校Service
 * @author 朱俊吉
 * @version 2017-12-12
 */
@Service
@Transactional(readOnly = true)
public class UniversityService extends CrudService<UniversityDao, University> {

	public University get(String id) {
		University university = super.get(id);
		university.setLevelList(dao.getLevelList(university));
		return university;
	}
	
	public List<University> findList(University university) {
		return super.findList(university);
	}
	
	public Page<University> findPage(Page<University> page, University university) {
		return super.findPage(page, university);
	}
	/**
	 * 高校大全搜索
	 * @param page
	 * @param university
	 * @return
	 */
	public Page<University> findCompletePage(Page<University> page, University university) {
		university.setPage(page);
		page.setList(dao.findCompletePage(university));
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(University university) {
		//将ckditor文本编辑框内容转换为DOM
		university.setIntro(StringEscapeUtils.unescapeHtml4(university.getIntro()));
		if (StringUtils.isBlank(university.getId())){
			university.preInsert();
			dao.insert(university);
		}else{
			// 更新大学数据
			university.preUpdate();
			dao.update(university);
		}
		if (StringUtils.isNotBlank(university.getId())){
			// 更新用户与角色关联
			dao.deleteUniversityLevel(university);
			if (university.getLevelList() != null && university.getLevelList().size() > 0){
				dao.insertUniversityLevel(university);
			}else{
				throw new ServiceException(university.getName() + "没有选择办学层次！");
			}
		}

	}
	
	@Transactional(readOnly = false)
	public void delete(University university) {
		super.delete(university);
	}

	/**
	 * 获取学校办学层次
	 * @param university
	 * @return
	 */
	public List<Dict> getLevelList(University university) {
		return dao.getLevelList(university);
	}
	
}